
//
//  UITextField+QTextField.m
//  QoilPublic
//
//  Created by 杨凯 on 16/12/22.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "UITextField+QTextField.h"


static const void *UItextfieldDelegateKey  = &UItextfieldDelegateKey;

@implementation UITextField (QTextField)


+ (void)load {
    
   
    SEL origin1 = @selector(init);
    SEL origin2 = @selector(initWithFrame:);
    SEL origin3 = @selector(awakeFromNib);
    
    SEL swizz1 = @selector(q_init);
    SEL swizz2 = @selector(q_initWithFrame:);
    SEL swizz3 = @selector(q_awakeFromNib);
    
    [self swizzleMethods:[self class] originalSelector:origin1 swizzledSelector:swizz1];
    [self swizzleMethods:[self class] originalSelector:origin2 swizzledSelector:swizz2];
    [self swizzleMethods:[self class] originalSelector:origin3 swizzledSelector:swizz3];
    
}

+ (void)swizzleMethods:(Class)class originalSelector:(SEL)origSel swizzledSelector:(SEL)swizSel
{
    Method origMethod = class_getInstanceMethod(class, origSel);
    Method swizMethod = class_getInstanceMethod(class, swizSel);
    
    BOOL didAddMethod = class_addMethod(class, origSel, method_getImplementation(swizMethod), method_getTypeEncoding(swizMethod));
    if (didAddMethod) {
        class_replaceMethod(class, swizSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    } else {
        method_exchangeImplementations(origMethod, swizMethod);
    }
}

- (instancetype)q_init{
    
    id __self = [self q_init];
    
    self.returnKeyType = UIReturnKeyDone;
   
    
    return __self;
}

- (instancetype)q_initWithFrame:(CGRect)rect{
    
    id __self = [self q_initWithFrame:rect];
    
    self.returnKeyType = UIReturnKeyDone;
    
    UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, kScreenWidth,44)];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 60, 7,50, 30)];
    [button setTitle:@"完成"forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(handleFinishBtn) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    UIBarButtonItem *flexibleitem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:self action:nil];
    NSArray *items = @[flexibleitem,flexibleitem,item];
    [bar setItems:items animated:YES];
    self.inputAccessoryView = bar;
    
    return __self;
}

-(void)q_awakeFromNib{
    [self q_awakeFromNib];
    self.returnKeyType = UIReturnKeyDone;
    
    UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, kScreenWidth,44)];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 60, 7,50, 30)];
    [button setTitle:@"完成"forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(handleFinishBtn) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    UIBarButtonItem *flexibleitem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:self action:nil];
    NSArray *items = @[flexibleitem,flexibleitem,item];
    [bar setItems:items animated:YES];
    self.inputAccessoryView = bar;
}
- (void)handleFinishBtn{
    
    [self resignFirstResponder];
    
}
@end
